package com.yupi.dada.config;

import com.zhipu.oapi.ClientV4;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import okhttp3.OkHttpClient;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

@Configuration
@ConfigurationProperties(prefix = "ai")
@Data
public class AiConfig {
    /**
     * apikey,需要从开放平台获取
     */
    private String apiKey;

    /**
     * 在 Spring 容器初始化时，将 KeyConstant.KEY 的值赋给 apiKey
     */
    @PostConstruct
    public void init() {
        // 完全依赖环境变量，不再回退到硬编码
        if (apiKey == null || apiKey.isEmpty()) {
            throw new IllegalArgumentException("AI_API_KEY 未配置");
        }
    }

    @Bean
    public ClientV4 getClientV4() {
        return new ClientV4.Builder(apiKey)
                .enableTokenCache()
                .networkConfig(60, 30, 30, 30, TimeUnit.SECONDS) // 增加所有超时参数
                .connectionPool(new okhttp3.ConnectionPool(8, 1, TimeUnit.MINUTES)) // 增加连接池超时
                .build();
    }

}